import { Resolver, Query, Args, ResolveProperty, Parent } from '@nestjs/graphql';
import { ArchiveService } from './archive.service';
import { Archive } from '../../graphql';
import { UserService } from '../../user/user.service';
import { ChannelService } from '../channel/channel.service';
import { ModelService } from '../model/model.service';
import { SpecialService } from '../special/special.service';

@Resolver('Archive')
export class ArchiveResolver {

  constructor(private readonly archiveService: ArchiveService,
              private readonly userService: UserService,
              private readonly channelService: ChannelService,
              private readonly specialService: SpecialService,
              private readonly modelService: ModelService) {}

  @Query('archive')
  getArchive(@Args('id') id: number) {
    return this.archiveService.findOneById(id);
  }

  @ResolveProperty('user')
  getUser(@Parent() parent: Archive) {
    const { user_id } = parent;
    if (user_id) {
      return this.userService.findOneById(user_id);
    }
  }

  @ResolveProperty('channel')
  getChannel(@Parent() parent: Archive) {
    const { channel_id } = parent;
    if (channel_id) {
      return this.channelService.findOneById(channel_id);
    }
  }

  @ResolveProperty('special')
  getSpecial(@Parent() parent: Archive) {
    const { special_id } = parent;
    if (special_id) {
      return this.specialService.findOneById(special_id);
    }
  }

  @ResolveProperty('model')
  getModel(@Parent() parent: Archive) {
    const { model_id } = parent;
    if (model_id) {
      return this.modelService.findOneById(model_id);
    }
  }

  @Query('archives')
  async getArchives(@Args() args: { page: number, pageSize: number }) {
    const { page = 1, pageSize = 20 } = args;
    const result = this.archiveService.findWithPagination(page, pageSize);
    return result;
  }
}
